let WebIM = require("../../utils/WebIM.js")["default"];

let msgType = require("./msgtype.js");

module.exports = function (sendableMsg, type, myName,receiveMsg) {
  var time = WebIM.time();
  const userForm = sendableMsg.body.from.toLocaleLowerCase()
  const userTo = sendableMsg.body.to.toLocaleLowerCase()
  var renderableMsg = {
    info: {
      from: userForm,
      to:userTo
    },
    username: userForm == myName ? userTo : userForm,
    yourname: userForm,
    msg: {
      type: type,
      url: sendableMsg.body.url ? sendableMsg.body.url : '',
      data: getMsgData(sendableMsg, type)
    },
    style: userForm == myName ? "self" : "",
    time: time,
    mid: sendableMsg.type + sendableMsg.id,
    chatType: sendableMsg.body.chatType,
		data:receiveMsg || sendableMsg.body
  };
  if (type == msgType.IMAGE) {
    renderableMsg.msg.size = {
      width: sendableMsg.body.body.size.width,
      height: sendableMsg.body.body.size.height
    };
  } else if (type == msgType.AUDIO) {
    renderableMsg.msg.length = sendableMsg.body.length;
  } else if (type == msgType.FILE) {
    renderableMsg.msg.data = [{
      data: "[当前不支持此格式消息展示]",
      type: "txt"
    }];
    renderableMsg.msg.type = 'txt';
  }

  return renderableMsg;

  function getMsgData(sendableMsg, type) {
    if (type == msgType.TEXT) {
      return WebIM.parseEmoji(sendableMsg.value.replace(/\n/mg, ""));
    } else if (type == msgType.EMOJI) {
      return sendableMsg.value;
    } else if (type == msgType.IMAGE || type == msgType.VIDEO || type == msgType.AUDIO) {
      return sendableMsg.body.body&&sendableMsg.body.body.url;
    } else if (type == msgType.FILE) {
      return sendableMsg.body.body&&sendableMsg.body.body.msg;
    }

    return "";
  }
};